package com.atguigu.ggkt.wechat.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.ggkt.model.wechat.Menu;
import com.atguigu.ggkt.swagger.exception.GgktException;
import com.atguigu.ggkt.vo.wechat.MenuVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.ggkt.wechat.service.MenuService;
import com.atguigu.ggkt.wechat.mapper.MenuMapper;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 欣欣
* @description 针对表【menu(订单明细 订单明细)】的数据库操作Service实现
* @createDate 2022-09-26 08:30:07
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService{
    @Autowired
    private WxMpService wxMpService;
    /**
     * 获取一级菜单列表和二级菜单列表
     * @return
     */
    @Override
    public List<MenuVo> findMenuInfo() {
        //1 获取一级菜单
        List<Menu> menuOneInfo = findMenuOneInfo();
        //2 获取二级菜单并相应的放入一级菜单内
        List<MenuVo> menuVoList = menuOneInfo.stream().map(menuOne -> {
            //获取一级MenuVo
            MenuVo menuVoOne = new MenuVo();
            BeanUtils.copyProperties(menuOne, menuVoOne);
            //构造条件器查询一级MenuVo对应的二级MenuVo
            LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Menu::getParentId, menuOne.getId());
            queryWrapper.orderByAsc(Menu::getSort);
            //获取对应的二级MenuVo
            List<MenuVo> menuTwoInfo = list(queryWrapper).stream().map(menuTwo -> {
                MenuVo menuVoTwo = new MenuVo();
                BeanUtils.copyProperties(menuTwo, menuVoTwo);
                return menuVoTwo;
            }).collect(Collectors.toList());
            //将对应二级MenuVo放入一级MenuVo
            menuVoOne.setChildren(menuTwoInfo);
            return menuVoOne;
        }).collect(Collectors.toList());
        return menuVoList;
    }

    /**
     * 获取一级菜单列表
     * @return
     */
    @Override
    public List<Menu> findMenuOneInfo() {
        LambdaQueryWrapper<Menu> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId,0);
        List<Menu> list = list(queryWrapper);
        return list;
    }

    /**
     * 菜单同步
     */
    @Override
    public void syncMenu() {
        //获得一级菜单和二级菜单
        List<MenuVo> menuVoList = this.findMenuInfo();
        //菜单 获得button
        JSONArray buttonList = new JSONArray();
        for(MenuVo oneMenuVo : menuVoList) {
            JSONObject one = new JSONObject();
            one.put("name", oneMenuVo.getName());
            //sub_button
            JSONArray subButton = new JSONArray();
            for(MenuVo twoMenuVo : oneMenuVo.getChildren()) {
                JSONObject view = new JSONObject();
                view.put("type", twoMenuVo.getType());
                if(twoMenuVo.getType().equals("view")) {
                    view.put("name", twoMenuVo.getName());
                    view.put("url", "http://ggkt2.vipgz1.91tunnel.com/#"
                            +twoMenuVo.getUrl());
                } else {
                    view.put("name", twoMenuVo.getName());
                    view.put("key", twoMenuVo.getMeunKey());
                }
                subButton.add(view);
            }
            one.put("sub_button", subButton);
            buttonList.add(one);
        }
        //菜单
        JSONObject button = new JSONObject();
        button.put("button", buttonList);
        try {
            this.wxMpService.getMenuService().menuCreate(button.toJSONString());
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除菜单
     */
    @Override
    public void removeMenu() {
        try {
            wxMpService.getMenuService().menuDelete();
        } catch (WxErrorException e) {
            throw new GgktException(20001,"删除菜单失败");
        }
    }
}




